

import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;

public class SVGWriter 
 {
  private String nameFile = null;
  private FileWriter writer = null;
  private int width = 1500;
  private int height = 1500;
  private String title = "";
  private String description = "";
  private String figure ="";
  private String enTete = "";
  private String finishFile = "";
  private String [] tabCouleur = {"black","red","blue","green"};

  public SVGWriter (String nameFile) throws IOException 
   {
    try
     {
      this.nameFile = nameFile;
      java.io.File fichier = new java.io.File(nameFile+".svg");
   
      if (fichier.exists())
       {
        fichier.delete();
       }
   
      fichier.createNewFile();
     }
    catch (IOException e)
     {
      System.out.println("Impossible de créer le fichier");
     }
   }

  public void write (String content) throws IOException
   {
    writer = new FileWriter(this.nameFile+".svg", true);
    writer.write(content,0,content.length());
   
    if(writer != null)
     {
      writer.close();
     }
   }

  public void writeAllData() throws IOException
   {
    setEnTete();
   
    if (enTete != "")
	 {
      write(enTete);
     }
    
    if (title != "")
	 {
      write(title);
     }
    
    if (description != "")
	 {
      write(description);
     }
     
    if(figure != "")
	 {
      dessineProcessus(1, "Test 1",0);
      dessineProcessus(2, "Test 2",0);
      dessineProcessus(3, "Test 3",0);
      dessineProcessus(4, "Test 4",0);
      dessineProcessus(5, "Test 5",0);
      write(figure);
     }
 
    setFinishFile();
   }

  public String getDescription()
   {
    return description;
   }

  public void setDescription(String myDescription)
   {
    this.description = "\n<desc>\n\t"+myDescription+"\n</desc>";
   }

  public String getTitle()
   {
    return title;
   }

  public void setTitle(String myTitle)
   {
    this.title = "\n<title>"+myTitle+"</title>";
   }

  public void setLine(int  x1line, int y1line, int x2line, int y2line) 
   {
    this.figure += "\n<line x1=\""+x1line+"\" y1=\""+y1line+"\" x2=\""+x2line+"\" y2=\""+y2line+"\" stroke=\"black\"/>";
   }

  public void setRectangle(int  widthRect, int heightRect, int xRect, int yRect) 
   {
    this.figure += "\n<rect fill = \"red\" width=\""+widthRect+"\" height=\""+heightRect+"\" x=\""+xRect+"\" y=\""+yRect+"\" />";
   }

  public void setFleche (int x1Fleche, int y1Fleche, int x2Fleche, int y2Fleche, int couleur)
   {
    this.figure += "\n<defs> \n\t<marker id = \"Triangle"+couleur+"\" viewBox = \"0 0 40 40\" refX = \"16\" refY = \"16\" markerWidth=\"3\" markerHeight=\"7\" fill=\""+tabCouleur[couleur]+"\" stroke=\""+tabCouleur[couleur]+"\" orient = \"auto\">"
            +"\n\t\t<path d = \"M 0 0 L 16 16 L 0 32 L 40 16 Z\" />"
            +"\n\t</marker>\n</defs>"
            +"\n<path d=\"M "+x1Fleche+" "+y1Fleche+"L "+x2Fleche+" "+y2Fleche+"\" "
            +"fill=\""+tabCouleur[couleur]+"\" stroke=\""+tabCouleur[couleur]+"\" stroke-width=\"5\" "
            +"marker-end=\"url(#Triangle"+couleur+")\"  />";
   }

  public void dessineMsg(int procDepart, int procArrive, int dateDepart, int dateArrive, int couleur)
   {
    setFleche(dateDepart, procDepart, dateArrive, procArrive,couleur);
   }

  public void dessineSC (int proc, int dateDebut, int dateFin)
   {
    int height = 10;
    setRectangle(dateFin-dateDebut, height, dateDebut, (proc*100)-height);
   }

  public void dessineProcessus(int num, String nom, int couleur)
   {
    this.figure += "\n <text stroke=\"black\" x=\"10\" y=\""+((num*100)-10)+"\">Proc "+num+"</text> ";
    setFleche(0, num * 100, 1000, num * 100, 0);
   }

  public void dessinerREQ (int numDep, int numArr, int dateDeb, int dateFin)
   {
    this.figure += "\n <text stroke=\"black\" x=\""+(dateDeb+10)+"\" y=\""+((numDep * 100)+20)+"\">"+dateDeb+"</text> ";
    setFleche(dateDeb, numDep * 100, dateFin, numArr * 100, 1);
    this.figure += "\n <text stroke=\"black\" x=\""+(dateFin+10)+"\" y=\""+((numArr * 100)+20)+"\">"+dateFin+"</text> ";
   }

  public void dessinerREP (int numDep, int numArr, int dateDeb, int dateFin)
   {
    this.figure += "\n <text stroke=\"black\" x=\""+(dateDeb*20+10)+"\" y=\""+((numDep * 100)+20)+"\">"+dateDeb+"</text> ";
    setFleche(dateDeb*20, numDep * 100, dateFin*20, numArr * 100, 2);
    this.figure += "\n <text stroke=\"black\" x=\""+(dateFin*20+10)+"\" y=\""+((numArr * 100)+20)+"\">"+dateFin+"</text> ";
   }

  public void dessinerREAL (int numDep, int numArr, int dateDeb, int dateFin)
   {
    this.figure += "\n <text stroke=\"black\" x=\""+(dateDeb+10)+"\" y=\""+((numDep * 100)+20)+"\">"+dateDeb+"</text> ";
    setFleche(dateDeb, numDep * 100, dateFin, numArr * 100, 3);
    this.figure += "\n <text stroke=\"black\" x=\""+(dateFin+10)+"\" y=\""+((numArr * 100)+20)+"\">"+dateFin+"</text> ";
   }

  public int getHeight()
   {
    return height;
   }

  public void setHeight(int height)
   {
    this.height = height;
   }

  public int getWidth()
   {
    return width;
   }

  public void setWidth(int width)
   {
    this.width = width;
   }

  public String getEnTete()
   {
    return enTete;
   }

  public void setEnTete()
   {
    this.enTete = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n<svg"+
            "\n\txmlns=\"http://www.w3.org/2000/svg\""+
            "\n\tversion=\"1.1\""+
            "\n\twidth=\""+this.getWidth()+"\""+
            "\n\theight=\""+this.getHeight()+"\">";
   }

  public String getFinishFile()
   {
    return finishFile;
   }

  public void setFinishFile() throws IOException
   {
    this.finishFile = "\n</svg>";
    write(finishFile);
   }
 }
